'************************************************************************************
' Copyright  2003-2005 Kathleen Dollard
'
' This software is provided 'as-is', without any express or implied warranty. In no 
' event will the author be held liable for any damages arising from the use of this
' software.
' 
' You may use this software for any purpose, including commercial applications, 
' and you may redistribute this software as part of application delivery: however, 
' this copyright notice may not be removed and you may not otherwise redistribute 
' this software directly other than providing a link to wwww.gendotnet.com. 
'
' You may alter this software to better server your needs (I'd like to hear about your 
' changes in order to make the software better). If you alter the softwre, you must  
' clearly mark it as altered and must not be misrepresented as the original software.
' nor wholly your original work.
'*************************************************************************************
'  Summary: Tools to facilitate working with navigator approach to XML DOM
'  Refactor: Since Whidbey will prefer the navigator approach, this class needs updating for transfer into Whidbey.

Option Explicit On 
Option Strict On

Imports System

Namespace KADGen.Utility
   Public Class NodeItHelpers
      Public Shared Function GetNodeIt( _
                  ByVal xnav As Xml.XPath.XPathNavigator, _
                  ByVal xPath As String, _
                  ByVal nsmgr As Xml.XmlNamespaceManager) _
                  As Xml.XPath.XPathNodeIterator
         Dim xpathExpr As Xml.XPath.XPathExpression = xnav.Compile(xPath)
         If Not nsmgr Is Nothing Then
            xpathExpr.SetContext(nsmgr)
         End If
         Return xnav.Select(xpathExpr)
      End Function

      Public Shared Function GetSortedNodeIt( _
               ByVal xnav As Xml.XPath.XPathNavigator, _
               ByVal xPath As String, _
               ByVal nsmgr As Xml.XmlNamespaceManager, _
               ByVal sortXpath As String, _
               ByVal isAscending As Boolean, _
               ByVal isCaseSensitive As Boolean, _
               ByVal isNumber As Boolean) _
               As Xml.XPath.XPathNodeIterator
         Dim xpathExpr As Xml.XPath.XPathExpression = xnav.Compile(xPath)
         Dim sortOrder As Xml.XPath.XmlSortOrder
         Dim caseOrder As Xml.XPath.XmlCaseOrder
         Dim datatype As Xml.XPath.XmlDataType
         If isAscending Then
            sortOrder = Xml.XPath.XmlSortOrder.Ascending
         Else
            sortOrder = Xml.XPath.XmlSortOrder.Descending
         End If
         If isCaseSensitive Then
            caseOrder = Xml.XPath.XmlCaseOrder.UpperFirst
         Else
            caseOrder = Xml.XPath.XmlCaseOrder.None
         End If
         If isNumber Then
            datatype = Xml.XPath.XmlDataType.Number
         Else
            datatype = Xml.XPath.XmlDataType.Text
         End If
         xpathExpr.AddSort(sortXpath, sortOrder, caseOrder, "", datatype)
         If Not nsmgr Is Nothing Then
            xpathExpr.SetContext(nsmgr)
         End If
         Return xnav.Select(xpathExpr)
      End Function

      Public Shared Function GetChildAttribute( _
                  ByVal xnav As Xml.XPath.XPathNavigator, _
                  ByVal childName As String, _
                  ByVal attributeName As String, _
                  ByVal nsmgr As Xml.XmlNamespaceManager) _
                  As String
         Dim nodeit As Xml.XPath.XPathNodeIterator = GetNodeIt(xnav, childName, nsmgr)
         nodeit.MoveNext()
         Return CStr(nodeit.Current.GetAttribute(attributeName, ""))
      End Function
   End Class
End Namespace